package com.example.ch01androidspringboot.controller;

import com.example.ch01androidspringboot.enetity.User;
import com.example.ch01androidspringboot.service.UserService;
import com.example.ch01androidspringboot.service.serviceImpl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/user")
    public void u(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("服务端被访问");
        ServletOutputStream outputStream=null;


        if (username.equals("1")){
            try {

                outputStream= response.getOutputStream();
                outputStream.write("success".getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                outputStream.write("error".getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //查询所有用户信息
    @RequestMapping("/alluser")
    public List<User> alluser(){
        System.out.println("进入查询所有用户信息接口");
        List<User> users = userService.selectUser();
        System.out.println(users);
        return users;
    }

    //
    @RequestMapping("/adduser")
    public int adduser(User user){
        User user1 = new User();
        int i = userService.insertUser(user1.getUsername(), user1.getPassword(), user1.getEmail());
        return i;
    }


}
